package com.caiyi.financial.nirvana.sms.gateway.sgip.message.body;

import com.caiyi.financial.nirvana.sms.gateway.cmpp.CMPPMessageTool;
import com.caiyi.financial.nirvana.sms.gateway.cmpp.CharsetInfo;
import com.caiyi.financial.nirvana.sms.gateway.cmpp.LongMsgHeader;
import com.caiyi.financial.nirvana.sms.gateway.sgip.message.SGIPBody;
import io.netty.buffer.ByteBuf;

public class SGIPSubmit extends SGIPBody implements Cloneable {

    private String spNumber;        //SP的接入号码
    private String chargeNumber = "";    //付费号码
    private String[] userNumber;    //手机号
    private String corpId = "11111";    //企业代码，取值范围0-99999 TODO
    private String serviceType = "HELP";//业务代码，由SP定义
    private int feeType = 1;    //计费类型
    private String feeValue = "0";    //该条短消息的收费值,单位为分
    private String givenValue = "0";    //赠送用户的话费,单位为分
    private int AgentFlag = 0;    //代收费标志，0：应收；1：实收
    private int morelatetoMTFlag = 2;    //引起MT消息的原因,2-非MO点播引起的MT消息；3-系统反馈引起的MT消息。
    private int priority = 0;        //优先级0-9从低到高，默认为0
    private String expireTime = "";    //短消息寿命的终止时间，如果为空，表示使用短消息中心的缺省值。格式为”yymmddhhmmsstnnp” ，其中“tnnp”取固定值“032+”，即默认系统为北京时间
    private String scheduleTime = "";    //短消息定时发送的时间，如果为空，表示立刻发送该短消息。格式同上
    private int reportFlag = 1;        //状态报告标记, 1-该条消息无论最后是否成功都要返回状态报告
    private byte tpPid = (byte) 0;    //GSM协议类型。详细解释请参考GSM03.40中的9.2.3.9
    private byte tpUdhi = (byte) 0;    //GSM协议类型。详细解释请参考GSM03.40中的9.2.3.23
    private CharsetInfo messageFormat = CharsetInfo.UCS2; //短消息的编码格式 ,15: GBK编码
    private int messageType = 0;    //信息类型：0-短消息信息 ,其它：待定
    private String messageContent;    //短消息的内容
    private String reserve = new String(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});    //保留位
    private LongMsgHeader msgHeader = null;

    @Override
    public int encode(ByteBuf buffer) {

        int size = 0;

        CMPPMessageTool.writeBytes2Buffer(buffer, spNumber.getBytes(), 21);
        size += 21;

        CMPPMessageTool.writeBytes2Buffer(buffer, chargeNumber.getBytes(), 21);
        size += 21;

        buffer.writeByte(userNumber.length);
        size += 1;

        for (String phoneNumber : userNumber) {
            CMPPMessageTool.writeBytes2Buffer(buffer, phoneNumber.getBytes(), 21);
            size += 21;
        }

        CMPPMessageTool.writeBytes2Buffer(buffer, corpId.getBytes(), 5);
        size += 5;

        CMPPMessageTool.writeBytes2Buffer(buffer, serviceType.getBytes(), 10);
        size += 10;

        buffer.writeByte(feeType);
        size += 1;

        CMPPMessageTool.writeBytes2Buffer(buffer, feeValue.getBytes(), 6);
        size += 6;

        CMPPMessageTool.writeBytes2Buffer(buffer, givenValue.getBytes(), 6);
        size += 6;

        buffer.writeByte(AgentFlag);
        size += 1;

        buffer.writeByte(morelatetoMTFlag);
        size += 1;

        buffer.writeByte(priority);
        size += 1;

        CMPPMessageTool.writeBytes2Buffer(buffer, expireTime.getBytes(), 16);
        size += 16;

        CMPPMessageTool.writeBytes2Buffer(buffer, scheduleTime.getBytes(), 16);
        size += 16;

        buffer.writeByte(reportFlag);
        size += 1;

        buffer.writeByte(tpPid);
        size += 1;

        buffer.writeByte(tpUdhi);
        size += 1;

        buffer.writeByte(messageFormat.getCode());
        size += 1;

        buffer.writeByte(messageType);
        size += 1;

        byte[] byte_content = messageFormat.encode(messageContent);
        int contentLen = byte_content.length;
        if (msgHeader != null) {
            contentLen += 6;
        }
        buffer.writeInt(contentLen);
        size += 4;

        if (msgHeader != null) {
            buffer.writeBytes(msgHeader.getBytes());
        }

        buffer.writeBytes(byte_content);
        size += contentLen;

        buffer.writeBytes(reserve.getBytes());
        size += 8;

        return size;
    }


    @Override
    public SGIPBody decode(ByteBuf buffer) {
        throw new UnsupportedOperationException("not need");
    }

    public String getSpNumber() {
        return spNumber;
    }


    public void setSpNumber(String spNumber) {
        this.spNumber = spNumber;
    }


    public String getChargeNumber() {
        return chargeNumber;
    }


    public void setChargeNumber(String chargeNumber) {
        this.chargeNumber = chargeNumber;
    }


    public String[] getUserNumber() {
        return userNumber;
    }


    public void setUserNumber(String[] userNumber) {
        this.userNumber = userNumber;
    }


    public String getCorpId() {
        return corpId;
    }


    public void setCorpId(String corpId) {
        this.corpId = corpId;
    }


    public String getServiceType() {
        return serviceType;
    }

    public void setServiceType(String serviceType) {
        this.serviceType = serviceType;
    }


    public int getFeeType() {
        return feeType;
    }


    public void setFeeType(int feeType) {
        this.feeType = feeType;
    }


    public String getFeeValue() {
        return feeValue;
    }


    public void setFeeValue(String feeValue) {
        this.feeValue = feeValue;
    }


    public String getGivenValue() {
        return givenValue;
    }


    public void setGivenValue(String givenValue) {
        this.givenValue = givenValue;
    }


    public int getAgentFlag() {
        return AgentFlag;
    }


    public void setAgentFlag(int agentFlag) {
        AgentFlag = agentFlag;
    }


    public int getMorelatetoMTFlag() {
        return morelatetoMTFlag;
    }


    public void setMorelatetoMTFlag(int morelatetoMTFlag) {
        this.morelatetoMTFlag = morelatetoMTFlag;
    }


    public int getPriority() {
        return priority;
    }


    public void setPriority(int priority) {
        this.priority = priority;
    }


    public String getExpireTime() {
        return expireTime;
    }


    public void setExpireTime(String expireTime) {
        this.expireTime = expireTime;
    }


    public String getScheduleTime() {
        return scheduleTime;
    }


    public void setScheduleTime(String scheduleTime) {
        this.scheduleTime = scheduleTime;
    }


    public int getReportFlag() {
        return reportFlag;
    }


    public void setReportFlag(int reportFlag) {
        this.reportFlag = reportFlag;
    }


    public int getMessageType() {
        return messageType;
    }


    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }


    public void setMessageFormat(CharsetInfo messageFormat) {
        this.messageFormat = messageFormat;
    }


    /**
     * @return the tpPid
     */
    public byte getTpPid() {
        return tpPid;
    }

    /**
     * @param tpPid the tpPid to set
     */
    public void setTpPid(byte tpPid) {
        this.tpPid = tpPid;
    }

    /**
     * @return the tpUdhi
     */
    public byte getTpUdhi() {
        return tpUdhi;
    }

    /**
     * @param tpUdhi the tpUdhi to set
     */
    public void setTpUdhi(byte tpUdhi) {
        this.tpUdhi = tpUdhi;
    }


    /**
     * @return the messageContent
     */
    public String getMessageContent() {
        return messageContent;
    }

    /**
     * @param messageContent the messageContent to set
     */
    public void setMessageContent(String messageContent) {
        this.messageContent = messageContent;
    }

    /**
     * 短消息编码
     */
    public CharsetInfo getMessageFormat() {
        return messageFormat;
    }

    public LongMsgHeader getMsgHeader() {
        return msgHeader;
    }

    public void setMsgHeader(LongMsgHeader msgHeader) {
        this.msgHeader = msgHeader;
    }


    @Override
    public Object clone() {

        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {

        }
        return null;
    }

}
